package dragonsource
{
	import com.dynamicflash.util.Base64;
	import com.greensock.TweenLite;
	import dragonsource.HintDog;
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	
	/**
	 * 本程序只做客户端，即直接连接到服务器，接受控制信号。
	 */
	public class SignalInputManager extends EventDispatcher
	{
		static public const UPDATE_STATUS:String = "c09e8872-c3a2-476e-9f36-83bcf1e452c9";
		private var __socket:Socket;
		private var response:String;
		public var currentBlocksStatus:Array = [];
		//
		public function SignalInputManager(host:String = null, port:uint = 0)
		{
			__socket = new Socket
			configureListeners();
			if (host && port)
			{
				__socket.connect(host, port);
			}
			
		}
		
		private function configureListeners():void
		{
			__socket.addEventListener(Event.CLOSE, closeHandler);
			__socket.addEventListener(Event.CONNECT, connectHandler);
			__socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			__socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			__socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
		}
		
		private function readResponse():void
		{
			var base64:String = __socket.readUTFBytes(__socket.bytesAvailable);
			//trace(base64); return;
			var ba:ByteArray = Base64.decodeToByteArray(base64);
			//行数
			var wid:int=(ba.readUnsignedByte() + (ba.readUnsignedByte() << 8));
			//列数
			var hei:int=(ba.readUnsignedByte()+(ba.readUnsignedByte()<<8));
			//todo 不缓冲自理，认为一定可以接到一个包。
			var rst:Array = [];
			var tmpba:int;
			var rsts:String = '';
			while (ba.bytesAvailable) {
				tmpba = ba.readUnsignedByte();
				var bu:String = '';
				for (var i:int = 0; i < 8 - tmpba.toString(2).length; i++) 
				{
					bu += '0';
				}
				rsts += (bu + tmpba.toString(2));
				/*11000001
					00011000
					01100001
					10000000
					00110110
					00000000*/
			}
			trace(rsts);
			for (var j:int = 0; j < rsts.length; j++) 
			{
				rst[j] = rsts.charAt(j)=='1'?true:false;
				//trace(j,rsts.charAt(j));
			}
			//trace(rst);
			currentBlocksStatus = rst;
			//true,true,false,false,false,false,false,true,false,false,false,true,true,false,false,false,false,true,true,false,false,false,false,true,true,false,false,false,false,false,false,false,false,false,true,true,false,true,true,false,false,false,false,false,false,false,false,false
			dispatchEvent(new Event(UPDATE_STATUS));
		}
		
		private function closeHandler(event:Event):void
		{
			trace("closeHandler: " + event);
			trace(response.toString());
		}
		
		private function connectHandler(event:Event):void
		{
			trace("connectHandler: " + event);
			HintDog.instance.setContent("Connected!!!!");
			TweenLite.delayedCall(8, clearHintHandler);
		}
		
		private function clearHintHandler():void 
		{
			HintDog.instance.setContent('');
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void
		{
			trace("ioErrorHandler: " + event);
			HintDog.instance.setContent("ioErrorHandler: ");
		}
		
		private function securityErrorHandler(event:SecurityErrorEvent):void
		{
			trace("securityErrorHandler: " + event);
		}
		
		private function socketDataHandler(event:ProgressEvent):void
		{
			trace("socketDataHandler: " + event);
			readResponse();
		}
	}
}